package com.computercourse.learning.mq;

import com.alibaba.fastjson.JSON;
import com.computercourse.framework.domain.task.CicTask;
import com.computercourse.framework.model.response.ResponseResult;
import com.computercourse.learning.config.RabbitMQConfig;
import com.computercourse.learning.service.LearningService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @description:
 * @author: yuanfeng
 * @date: Created in 2020/2/15 4:15 下午
 * @version: 1.0
 */
@Component
public class ChooseCourseTask {


    @Autowired
    private LearningService learningService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = RabbitMQConfig.CIC_LEARNING_ADDCHOOSECOURSE)
    public void receiveChooseCourseTask(CicTask cicTask){

        //取出消息的内容
        String requestBody = cicTask.getRequestBody();

        Map map = JSON.parseObject(requestBody, Map.class);
        String userId = (String) map.get("userId");
        String courseId = (String) map.get("courseId");

        //添加选课
        ResponseResult addCourse = learningService.addCourse(userId, courseId, null, null, null, cicTask);

        if(addCourse.isSuccess()){

            //添加选课成功，要向mq发送完成添加选课的信息
            rabbitTemplate.convertAndSend(RabbitMQConfig.EX_LEARNING_ADDCHOOSECOURSE,RabbitMQConfig.CIC_LEARNING_FINISHADDCHOOSECOURSE_KEY);

        }

    }


}